package cease.butter;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSON;

import org.apache.commons.lang.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import cease.console.PrintHelper;
import cease.urs.LoginException;
import cease.urs.Loginer;
import cease.urs.Session;
import cease.vo.Butter;
import cease.vo.UnreadsCount;

/**
 * @author dhf
 */
public class TimelineExecutor extends BaseExecutor {
    private static TimelineExecutor executor = new TimelineExecutor();

    public static TimelineExecutor getInstance() {
        return executor;
    }

    /**
     * 查看自己的timeline
     * 
     * @param session
     * @param size
     * @param cursor
     * @param sort
     *            只要sort不为null，则使用“同主题”排序模式
     * @return
     * @throws ApiException
     */
    public Collection<Butter> home(Session session, Integer size,
            String cursor, String sort) throws ApiException {
        if (null != size) {
            if (size <= 0) {
                size = 1;
            }
            if (size > 100) {
                size = 100;
            }
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        if (null != size) {
            pairs.add(new BasicNameValuePair("size", size + ""));
        }
        if (null != cursor) {
            pairs.add(new BasicNameValuePair("cursor", cursor));
        }
        if (null != sort) {
            pairs.add(new BasicNameValuePair("sort", sort));
        }
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_TIMELINE_HOME, pairs, null, session);
        return toButterArray(json);
    }

    /**
     * 查看公共的timeline
     * 
     * @param session
     * @return
     * @throws ApiException
     */
    public Collection<Butter> pub(Session session) throws ApiException {
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_TIMELINE_PUBLIC, pairs, null, session);
        return toButterArray(json);
    }

    /**
     * 查看指定用户的timeline
     * 
     * @param session
     * @param user
     *            用户id或者email
     * @param size
     * @param cursor
     * @param sort
     *            只要sort不为null，则使用“同主题”排序模式
     * @return
     * @throws ApiException
     */
    public Collection<Butter> user(Session session, String user, Integer size,
            String cursor, String sort) throws ApiException {
        if (null != size) {
            if (size <= 0) {
                size = 1;
            }
            if (size > 100) {
                size = 100;
            }
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        if (null != user) {
            pairs.add(new BasicNameValuePair("id", user));
        }
        if (null != size) {
            pairs.add(new BasicNameValuePair("size", size + ""));
        }
        if (null != cursor) {
            pairs.add(new BasicNameValuePair("cursor", cursor));
        }
        if (null != sort) {
            pairs.add(new BasicNameValuePair("sort", sort));
        }
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_TIMELINE_USER, pairs, null, session);
        return toButterArray(json);
    }

    /**
     * 查看“转发我的”
     * 
     * @param session
     * @param size
     * @param cursor
     * @return
     * @throws ApiException
     */
    public Collection<Butter> retweet(Session session, Integer size,
            String cursor) throws ApiException {
        if (null != size) {
            if (size <= 0) {
                size = 1;
            }
            if (size > 100) {
                size = 100;
            }
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        if (null != size) {
            pairs.add(new BasicNameValuePair("size", size + ""));
        }
        if (null != cursor) {
            pairs.add(new BasicNameValuePair("cursor", cursor));
        }
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_TIMELINE_RETWEET, pairs, null, session);
        return toButterArray(json);
    }

    /**
     * 查看“评论@我的”
     * 
     * @param session
     * @param size
     * @param cursor
     * @return
     * @throws ApiException
     */
    public Collection<Butter> mention(Session session, Integer size,
            String cursor) throws ApiException {
        if (null != size) {
            if (size <= 0) {
                size = 1;
            }
            if (size > 100) {
                size = 100;
            }
        }
        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        if (null != size) {
            pairs.add(new BasicNameValuePair("size", size + ""));
        }
        if (null != cursor) {
            pairs.add(new BasicNameValuePair("cursor", cursor));
        }
        pairs.add(new BasicNameValuePair("t", System.currentTimeMillis() + ""));
        JSON json = get(Constants.URL_TIMELINE_MENTION, pairs, null, session);
        return toButterArray(json);
    }

    /**
     * 未读消息数
     * 
     * @param session
     * @return
     * @throws ApiException
     */
    public UnreadsCount unreadsCount(Session session) throws ApiException {
        List<NameValuePair> params = new LinkedList<NameValuePair>();
        params
                .add(new BasicNameValuePair("t", System.currentTimeMillis()
                        + ""));
        JSON json = get(Constants.URL_NOTICE_COUNT, params, null, session);
        return toUnreadsCount(json);
    }

    /**
     * 传统意义上的搜索
     * 
     * @param session
     * @param query
     *            查询词
     * @param recordPerPage
     *            每次请求会返回多少条结果，默认值为20，最大值为100
     * @param pageIndex
     *            取第几页的结果，从0开始计数，recordPerPage * pageIndex不大于1500
     * @param since
     *            yyyy-MM-dd 格式的字符串，查询自指定时间之后的butter
     * @param until
     *            yyyy-MM-dd 格式的字符串，查询自指定时间之前的butter
     * @param showUser
     *            查询结果中是否需要包含用户信息
     * @return
     * @throws ApiException
     */
    public Collection<Butter> search(Session session, String query,
            Integer recordPerPage, Integer pageIndex, String since,
            String until, Boolean showUser) throws ApiException {
        if (StringUtils.isBlank(query)) {
            throw new IllegalArgumentException("[query] could not be null");
        }

        List<NameValuePair> pairs = new LinkedList<NameValuePair>();
        pairs.add(new BasicNameValuePair("q", query));
        if (null != recordPerPage) {
            pairs.add(new BasicNameValuePair("rpp", recordPerPage + ""));
        }
        if (null != pageIndex) {
            pairs.add(new BasicNameValuePair("page", pageIndex + ""));
        }
        if (null != since) {
            pairs.add(new BasicNameValuePair("since", since));
        }
        if (null != until) {
            pairs.add(new BasicNameValuePair("until", until));
        }
        if (null != showUser) {
            pairs.add(new BasicNameValuePair("show_user", showUser + ""));
        }
        JSON json = post(Constants.URL_BUTTER_POST, pairs, null, session);
        return toButterArray(json);
    }

    public static void main(String[] args) throws LoginException, ApiException {
        Session session = Loginer.login("dhf@126.com", "wrongpassword");
        // 未读消息数
        System.out.println(executor.unreadsCount(session));
        PrintHelper.printButter(executor.home(session, null, null, null));
        PrintHelper.printButter(executor.pub(session));
        PrintHelper.printButter(executor.mention(session, null, null));
        PrintHelper.printButter(executor.retweet(session, null, null));
        PrintHelper.printButter(executor.user(session, null, null, null, null));
    }
}
